const express = require("express");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const multer = require("multer");
// 引入自定义路由功能模块
const register = require("./modules/register");
const login = require("./modules/login");
const getUser = require("./modules/getuser");
const setUser = require("./modules/setuser");
const setPassword = require("./modules/setpassword");
const setAvatar = require("./modules/avatar");
const checkLogin = require("./modules/checklogin");
const importExcel = require("./modules/importexcel");
const getGoods = require("./modules/getgoods");
const addCart = require("./modules/addcart");
const getCart = require("./modules/getcart");
const setCart = require("./modules/setcart");
const allCheck = require("./modules/allcheck");

// 创建服务对象
const app = express();
// 注册静态资源中间件
app.use(express.static("./www"));
app.use(express.static("./upload"));
// 注册解析post数据中间件
app.use(express.urlencoded());
// 注册解析cookie数据中间件
app.use(cookieParser());
// 注册并配置session中间件
app.use(session({
    secret: '千锋',   // 加密因子，可以随便写
    resave: false,      // 强制保存session，默认为true，建议设置为false
    saveUninitialized: true,    // 强制将未初始化的session存储，默认为true，建议为true
    cookie: { maxAge: 1000 * 60 * 120 }    // 过期时间，毫秒数
}))
// 注册multer中间件，用来处理前端发送的文件
const upload = multer({ dest: "./upload" });
app.use(upload.any());

// 将路由功能绑定到指定路由
app.post("/register", register);
app.get("/login", login);
app.get("/getuser", getUser);
app.post("/setuser", setUser);
app.post("/setpassword", setPassword);
app.post("/setavatar", setAvatar);
app.get("/checklogin", checkLogin);
app.get("/importexcel", importExcel);
app.get("/getgoods", getGoods);
app.post("/addcart", addCart);
app.get("/getcart", getCart);
app.post("/setcart", setCart);
app.post("/allcheck", allCheck);

// 监听端口
app.listen("3000", ()=>{
    console.log("服务器开启成功，地址为：http://localhost:3000")
})